﻿@using Framework.HtmlHelpers
@using WebUI.Areas.Admin.Models.Common
@using WebUI.Areas.Admin.Models.EvaResultVM
@using WebUI.Extensions
@using Domain
@model EvaResultListViewModel
@{
	Layout = "~/Areas/Admin/Views/Shared/_AdminContent.cshtml";
	IList<NormType> allNormType = ViewBag.AllNormType;
	IList<int> allNormTypeId = allNormType.Select(m => m.ID).ToList();
}
@section head {
	<style>
		table tr td {
			vertical-align: middle !important;
		}
	</style>
}
@section body {
	<script src="~/dist/pview/pview.js"></script>
	<script>
		function showMessage(message, type) {
			$("#js-message").fadeIn();
			switch (type) {
				case -1:
					$("#js-message").attr("class", "alert alert-danger").find(".message").html(message);
					break;
				case 1:
					$("#js-message").attr("class", "alert alert-success").find(".message").html(message);
					break;
			}
			// 3s 后隐藏
			setTimeout(function () {
				$("#js-message").fadeOut("slow");
			}, 3000);
		}

		// start 计算分数
		function caculateScore(evaTaskId, teacherId, index) {
			$("#js-btn-caculate-" + index).html("计算中").attr("disabled", "disabled");
			// 新计算分数, 实际上就是根据 评价记录创建对应的评价结果
			var selectedEvaTaskId = $('#SelectedValForEvaTask').val();
			$.ajax({
				url: "/Admin/EvaResult/CaculateScore",
				type: "post",
				data: { evaTaskId: evaTaskId, teacherId: teacherId },
				dataType: "json",
				success: function (data) {
					$("#js-btn-caculate-" + index).html("计算分数").removeAttr("disabled");
					if (data.code > 0) {
						// 计算成功
						window.location.href = window.location.href;
					} else {
						showMessage(data.message, -1);
					}
				}
			});
		}
		// end 计算分数

		// start 搜索
		function search() {
			var query = $('#js-search-input').val();
			var queryType = $('#js-search-type').attr('search-type');
			window.location.href = window.location.origin + window.location.pathname + '?q=' + query + '&type=' + queryType;
		}
		// end 搜索

		// start 导出 excel
		function exportExcel() {
			var selectedVal = $('#SelectListForEvaTask').val();
			if (selectedVal == 0) {
				showMessage('请选择评价任务', -1);
				return;
			}
			$('#js-btn-export-excel').html('导出中...');
			$.ajax({
				url: "/Admin/EvaResult/ExportExcel",
				type: "post",
				data: { evaTaskId: selectedVal },
				dataType: "json",
				success: function (data) {
					$('#js-btn-export-excel').html('导出评价结果(Excel)');
					if (data.code <= 0) {
						showMessage(data.message, -1)
					} else {
						$('#js-file-export-excel').css('display', 'block').find('a').attr('href', data.fileUrl);
					}
				}
			});
		}
		// emd 导出 excel

		var pview;
		$(function () {
			pview = new PView();
			pview.init();

			// start 搜索
			$('#js-search-input').on('keypress', function (event) {
				if (event.keyCode == "13") {
					search();
				}
			});
			$('#js-search-btn').on('click', function () {
				search();
			});
			$('#js-search-type-list li').on('click', function () {
				var type = $(this).attr('search-type');
				var typeName = $(this).find('a').html();
				$('#js-search-type').attr('search-type', type);
				$('#js-search-type').html(typeName);
			});
			// end 搜索

			// start 导出 excel
			$('#js-btn-export-excel').on('click', exportExcel);
			// end 导出 excel
		});
	</script>
	@if (TempData["message"] != null)
	{
		<script>
			showMessage('@TempData["message"].ToString()', 1);
		</script>
	}
}
@helper GenerateScoreDesc(EvaResultVMItem currentVMItem, IList<int> allNormTypeId)
{
	foreach (var typeId in allNormTypeId)
	{
		// 当前分数明细 有哪些 评价类型 的分数
		IList<int> current_ScoreDic_NormType_idList = currentVMItem.ScoreDic.Keys.Select(m => m.ID).ToList();
		// 有此 评价类型的 (明细)分数
		if (current_ScoreDic_NormType_idList.Contains(typeId))
		{
			// 此 评价类型 的 分数
			<td>@(currentVMItem.ScoreDic.Where(m => m.Key.ID == typeId).FirstOrDefault().Value.ToString("0.##"))</td>
		}
		else
		{
			<td>未计算</td>
		}
	}
}

<!-- start 提示框 -->
@{ Html.RenderPartial("_MessagePartial"); }
<!-- end 提示框 -->

<input id="js-id" type="hidden" />

<!-- start .box -->
<div class="box">
	<!-- start .box-header -->
	<div class="box-header">
		<h3 class="box-title">列表</h3>

		<div class="box-tools">
			<div class="input-group input-group-sm" style="width: 250px;">
				<div class="input-group-btn">
					<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
						<span id="js-search-type" search-type="@(ViewBag.QueryType.Val)">@(ViewBag.QueryType.Text)</span>
						<span class="fa fa-caret-down"></span>
					</button>
					<ul id="js-search-type-list" class="dropdown-menu">
						<li search-type="teachername"><a href="javascript:;">教师名</a></li>
						<li search-type="employeecode"><a href="javascript:;">教师工号</a></li>
						<li search-type="evataskname"><a href="javascript:;">评价任务名</a></li>
						<li search-type="scoresum"><a href="javascript:;">总分</a></li>
					</ul>
				</div>
				<input id="js-search-input" type="text" name="table_search" class="form-control pull-right" placeholder="Search" value="@ViewBag.Query">
				<div class="input-group-btn">
					<button id="js-search-btn" type="submit" class="btn btn-default"><i class="fa fa-search"></i></button>
				</div>
			</div>
		</div>
	</div>
	<!-- end .box-header -->
	<!-- start .box-body -->
	<!-- start 简单表格 -->
	<div class="box-body table-responsive no-padding">
		<table id="js-table" class="table table-hover" pview="table">
			<tr>
				<th>序号</th>
				<th>评价任务</th>
				<th>被评教师（员工）</th>
				@foreach (var item in allNormType)
				{
					<th>@item.Name</th>
				}
				<th>总分</th>
				<th>操作</th>
			</tr>
			@foreach (var item in Model.List)
			{
				<tr>
					<td>@item.ID</td>
					<td>
						<span class="label label-info">@item.EvaTask.Name</span>
					</td>
					<td>@item.EvaedEmployee.Name（@item.EvaedEmployee.EmployeeCode）</td>
					@GenerateScoreDesc(item, allNormTypeId)
					<td>@item.ScoreSum.ToString("0.##")</td>
					<td>
						<div class="btn btn-group">
							<button id="js-btn-caculate-@item.ID" class="btn btn-primary" onclick="caculateScore(@item.EvaTask.ID, @item.EvaedEmployee.ID, @item.ID)">计算分数</button>
						</div>
					</td>
				</tr>
			}
		</table>
	</div>
	<!-- end 简单表格 -->
	<!-- end .box-body -->
	<!-- start .box-footer -->
	<div class="box-footer clearfix">
		<div class="row">
			<div class="col-xs-2">
				<div class="form-group" style="width: 160px;">
					<label>评价任务</label>
					@Html.DropDownList("SelectListForEvaTask", (IList<SelectListItem>)ViewBag.SelectListForEvaTask, new { @class = "form-control" })
				</div>
			</div>
			<div class="col-xs-4">
				<div class="btn-group" style="padding-top: 24px;">
					<button id="js-btn-export-excel" class="btn btn-primary">导出评价结果(Excel)</button>
					<span id="js-file-export-excel" style="display: none;transform: translate(170px,-26px);"><a href="#" target="_blank">立即下载Excel</a></span>
				</div>
			</div>
			<div class="col-xs-6">
				<!-- start 分页 -->
				<div id="js-page" pview="pages">
					<ul class="pagination pull-right" pview-btn-group pview-targets="table,pages">
						@Html.PageLinks(Model.PageInfo, i => Url.Action("Index", new { pageIndex = i, q = Request["q"], type = Request["type"] }))
					</ul>
				</div>
				<!-- end 分页 -->
			</div>
		</div>
	</div>
	<!-- end .box-footer -->
</div>
<!-- end .box -->
